#ifndef __STREAMFILE_SERVER_H__
#define __STREAMFILE_SERVER_H__

#include <string.h>
#include <map>
#include <mutex>
#include <functional>
#include "Util/util.h"
#include "Util/File.h"
#include "jsoncpp/json.h"
#include "Network/Buffer.h"

using namespace Fdt;
using namespace std;



class StreamFileServer{
public:
	static StreamFileServer *instance(){
		if(sm_streamfileServer == nullptr){
			lock_guard<mutex> lock(StreamFileServer::sm_mutex);
			sm_streamfileServer = new StreamFileServer();
		}
		return sm_streamfileServer;
	}
	
	~StreamFileServer();	
protected:
	StreamFileServer();
public:
	bool SetFileInfo(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);
	bool TransferStream(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);
	bool GetFileInfo(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);
	bool DownloadStream(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap);
	bool verifyStreamFile(const Json::Value &request, std::string &str_err);
	int stream_write(const void *buffer, size_t size);

private:
	static StreamFileServer * sm_streamfileServer;
	void RegistRPC();
	//接受http包用到的四个成员
	function<bool(string file, string url, function<void(const SockException &ex)> cb)> m_lam;
	string _file_name;
	string _type;
	string _md5;
	int _total_size;
	int _pos;
	size_t _size;
	static mutex sm_mutex;
};
#endif
